/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010, 2010

    This file is part of Maxsi Engine.

    Maxsi Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiEngineContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiEngineContributors.h. For more information please see
	MaxsiEngineContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiEngine.dll
	A linkable version of Maxsi Engine

	MaxsiCRuntime.cpp
	Turns out MinGW doesn't support as much useful stuff as Microsoft, so
	we have to implement it ourselves!

****************************************************************************/

#include "MaxsiEngine.h"

#ifdef __MINGW_H

namespace MaxsiEngine
{
	int strcat_s(char* strDestination, size_t numberOfElements, const char* strSource)
	{
		if ( strlen(strDestination) + strlen(strSource) +1 > numberOfElements )
		{
			// We should throw a warning here! For now, just crash
			char* DeletingThisWillMakeAHorribleCrash = "Yay!";
			delete[] DeletingThisWillMakeAHorribleCrash;

			return 0;
		} 

		strcat(strDestination, strSource);

		return 1;
	}

	int strcpy_s(char* strDestination, size_t numberOfElements, const char* strSource)
	{
		if ( strlen(strSource) + 1 < numberOfElements )
		{
			strcpy(strDestination, strSource);
			return 1;
		}
		else
		{
			return 0;
		}
	}

	int wcscpy_s(wchar_t* strDestination, size_t numberOfElements, const wchar_t* strSource)
	{
		if ( wcslen(strSource) + 1 < numberOfElements )
		{
			wcscpy(strDestination, strSource);
			return 1;
		}
		else
		{
			return 0;
		}
	}

	int	_itoa_s(int value, char *buffer, size_t sizeInCharacters, int radix)
	{
		if ( radix == 10 )
		{
			snprintf(buffer, sizeInCharacters, "%i", value);
		}
		else if  ( radix == 16 )
		{
			snprintf(buffer, sizeInCharacters, "%X", value);
		}
		else
		{
			return 0;
		}
		return 1;
	}

	int	_uitoa_s(int value, char *buffer, size_t sizeInCharacters, int radix)
	{
		if ( radix == 10 )
		{
			snprintf(buffer, sizeInCharacters, "%u", value);
		}
		else if  ( radix == 16 )
		{
			snprintf(buffer, sizeInCharacters, "%X", value);
		}
		else
		{
			return 0;
		}
		return 1;
	}

	int	_i64toa_s(__int64 value, char *buffer, size_t sizeInCharacters, int radix)
	{
		if ( radix == 10 )
		{
			snprintf(buffer, sizeInCharacters, "%I64i", value);
		}
		else if  ( radix == 16 )
		{
			snprintf(buffer, sizeInCharacters, "%I64X", value);
		}
		else
		{
			return 0;
		}
		return 1;
	}

	int	_ui64toa_s(__int64 value, char *buffer, size_t sizeInCharacters, int radix)
	{
		if ( radix == 10 )
		{
			snprintf(buffer, sizeInCharacters, "%I64u", value);
		}
		else if  ( radix == 16 )
		{
			snprintf(buffer, sizeInCharacters, "%I64X", value);
		}
		else
		{
			return 0;
		}
		return 1;
	}

	int _gcvt_s(char *buffer, size_t sizeInBytes, double value, int digits )
	{
		sprintf(buffer,"%.*g",sizeInBytes,value);
		return 1;
	}

	errno_t _gmtime64_s(struct tm* _tm, const __time64_t* time)
	{
		tm*	Result	=	_gmtime64(time);
		memcpy(_tm, Result, sizeof(tm));
		return 0;
	}

	errno_t asctime_s(char* buffer, size_t numberOfElements, const struct tm *_tm)
	{
		char* Time = asctime(_tm);

		if ( strlen(Time) + 1 > numberOfElements ) { return 1; }

		memcpy(buffer, Time, strlen(Time) + 1);

		return 0;
	}

}

#endif
